﻿using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Razor.TagHelpers;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;

namespace FwProject.Web.FwTagHelper
{
    [HtmlTargetElement("BoolSelect")]
    public class BoolSelectTagHelper : TagHelper
    {
        [HtmlAttributeName("asp-DataType")]
        public Type DataTypeValue { get; set; }

        [HtmlAttributeName("asp-value")]
        public string Value { get; set; }


        [HtmlAttributeName("asp-id")]
        public string Id { get; set; }

        [HtmlAttributeName("asp-name")]
        public string Name { get; set; }


        [HtmlAttributeName("asp-href")]
        public string DataHref { get; set; }

        [HtmlAttributeName("asp-valuetype")]
        public int ValueIsIndex { get; set; } =1;

        /// <summary>
        /// 将Enum转换为List<SelectListItem>
        /// </summary>
        /// <returns></returns>

        private List<SelectListItem> GetEnumSelectListItem()
        {
            var list = new List<SelectListItem>();
            if (DataTypeValue != null) {
                var typeInfo = DataTypeValue.GetTypeInfo();
                var enumValues = typeInfo.GetEnumValues();
                FieldInfo[] fields = DataTypeValue.GetFields(BindingFlags.Static | BindingFlags.Public);
                foreach (var value in enumValues)
                {

                    MemberInfo memberInfo =
                        typeInfo.GetMember(value.ToString()).First();


                    var descriptionAttribute =
                        memberInfo.GetCustomAttribute<DisplayAttribute>();

                    if (descriptionAttribute != null)
                    {
                        list.Add(new SelectListItem()
                        {
                            Text = descriptionAttribute.Name,
                            Value = (ValueIsIndex == 1) ? ((int)value).ToString() : value.ToString()
                        });
                    }
                    else
                    {
                        var descriptionNameAttribute =
                       memberInfo.GetCustomAttribute<DisplayNameAttribute>();
                        list.Add(new SelectListItem()
                        {
                            Text = descriptionNameAttribute.DisplayName,
                            Value = (ValueIsIndex == 1) ? ((int)value).ToString() : value.ToString()
                        });
                    }

                }
            }
         
            return list;
        }

        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            var Items = GetEnumSelectListItem();
            if (Items.Count ==0) {
                Items= new List<SelectListItem>()
               {
                new SelectListItem() { Text = "是", Value = "True" },
                new SelectListItem() { Text = "否", Value = "False"}
              };
      }

            output.TagName = "div";
            output.Attributes.SetAttribute("class", "col-md-12");
            foreach (var item in Items)
            {
                var radio = new TagBuilder("input");
                radio.TagRenderMode = TagRenderMode.SelfClosing;
          
                radio.Attributes.Add("name", Name);
                radio.Attributes.Add("value", item.Value);
                radio.Attributes.Add("title", item.Text);
                radio.Attributes.Add("type", "radio");
                radio.Attributes.Add("class", "i-checks");

                if (item.Disabled)
                {
                    radio.Attributes.Add("disabled", "disabled");
                }
                if (item.Selected || item.Value == Value)
                {
                    radio.Attributes.Add("checked", "checked");
                }
                output.Content.AppendHtml(radio);
                output.Content.AppendHtml(item.Text);
            }
        }

    }
}
